//
//  ClickEffectLayer.m
//  安好
//
//  Created by qinlongqing on 14/11/27.
//  Copyright (c) 2014年 lianchuangbrother. All rights reserved.
//

#import "ClickEffectLayer.h"


const CGFloat radius = 80;//圆的半径
const NSTimeInterval animationDuration = 1.5;//动画持续时间

@interface ClickEffectLayer ()
{
    CAAnimationGroup *animationGroup;
}
@end

@implementation ClickEffectLayer

- (instancetype)init{
    self = [super init];
    if (self) {
        self.backgroundColor = UIColorFromRGB(0xFF0000).CGColor;
        
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            [self setupAnimationGroup];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self addAnimation:animationGroup forKey:@"pulse"];
            });
        });
    }
    return self;
}

- (void)setCurrentPoint:(CGPoint)currentPoint{
    self.bounds = CGRectMake(0, 0, radius*2, radius*2);
    self.cornerRadius = radius;
    self.position = currentPoint;
}

- (void)setupAnimationGroup{
    animationGroup = [CAAnimationGroup animation];
    animationGroup.duration = animationDuration;
    animationGroup.repeatCount = INFINITY;//一直重复
    animationGroup.removedOnCompletion = NO;
    animationGroup.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault];
    
    CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale.xy"];
    scaleAnimation.fromValue = @0.0;
    scaleAnimation.toValue = @1.0;
    scaleAnimation.duration = animationDuration;
    
    CAKeyframeAnimation *opacityAnimation = [CAKeyframeAnimation animationWithKeyPath:@"opacity"];
    opacityAnimation.duration = animationDuration;
    opacityAnimation.values = @[@0.75,@0.45,@0];
    opacityAnimation.keyTimes = @[@0,@0.2,@1];
    opacityAnimation.removedOnCompletion = NO;
    
    animationGroup.animations = @[scaleAnimation,opacityAnimation];
}


@end
